﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MisClient
{
    public partial class FrmGoodsPrice : BaseForm
    {
        private IBLL.IDepart departBLL = MisCLL.DAOFactory.CreateInstance().GetRemoteDepart();
        private IBLL.ISacf sacfBLL = MisCLL.DAOFactory.CreateInstance().GetRemoteSacf();
        private IBLL.ICustomer cusBLL = MisCLL.DAOFactory.CreateInstance().GetRemoteCustomer();
        private IBLL.ICusDep cusDepBLL = MisCLL.DAOFactory.CreateInstance().GetRemoteCusDep();
        private IBLL.IFkType typeBLL = MisCLL.DAOFactory.CreateInstance().GetRemoteFkType();
        private IBLL.IGoods goodsBLL = MisCLL.DAOFactory.CreateInstance().GetRemoteGoods();

        public FrmGoodsPrice()
        {
            InitializeComponent();
        }

        private void FrmGoodsPrice_Load(object sender, EventArgs e)
        {
            this.openStarDate.Value = DateTime.Now.Date.AddDays(-30);
            this.openStarDate.Focus();
        }

        private void openStarDate_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.openEndDate.Focus();
            }
        }

        private void openEndDate_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.txtDepCode.Focus();
            }
        }

        private void txtDepCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (string.IsNullOrEmpty(this.txtDepCode.Text.Trim()))
                {
                    MessageBox.Show("部门代码不能为空！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.txtDepCode.Focus();
                    return;
                }
                MisModel.Depart depart = departBLL.GetById(this.txtDepCode.Text.Trim());
                if (depart == null)
                {
                    MessageBox.Show("此部门无权操作！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.txtDepCode.Focus();
                    return;
                }
                this.txtBoxDep.Text = depart.DepName;
                this.txtCusCode.Focus();
            }
        }

        private void txtCusCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (string.IsNullOrEmpty(this.txtCusCode.Text.Trim()))
                {
                    MessageBox.Show("客户代码不能为空！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.txtCusCode.Focus();
                    return;
                }
                MisModel.Customer customer = cusBLL.GetById(this.txtCusCode.Text.Trim());
                if (customer == null)
                {
                    MessageBox.Show("无此代码或不属于本部分！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.txtCusCode.Focus();
                    return;
                }
                this.txtBoxCus.Text = customer.Customer_;
                this.txtCusDepCode.Focus();
            }
        }

        private void txtCusDepCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (string.IsNullOrEmpty(this.txtCusDepCode.Text.Trim()))
                {
                    MessageBox.Show("客户部门不能为空！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.txtCusDepCode.Focus();
                    return;
                }
                MisModel.CusDep cusDep = cusDepBLL.GetBy(this.txtCusCode.Text.Trim(), this.txtCusDepCode.Text.Trim());
                if (cusDep == null)
                {
                    MessageBox.Show("客户部门代码不存在,或无权操作！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.txtCusDepCode.Focus();
                    return;
                }
                this.txtBoxCusDep.Text = cusDep.Depart;
                this.comboBox2.Focus();
            }
        }

        private void comboBox2_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.goodsName.Focus();
            }
        }

        private void txtFKID_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (string.IsNullOrEmpty(this.txtFKID.Text.Trim()))
                {
                    MessageBox.Show("结算类型不能为空！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.txtFKID.Focus();
                    return;
                }

                MisModel.FKType fkType = typeBLL.GetById(this.txtFKID.Text.Trim());
                if (fkType == null)
                {
                    MessageBox.Show("无此代码或不属于此结算类型！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.txtFKID.Focus();
                    return;
                }
                this.txtBoxFK.Text = fkType.FkType;
                this.txtID.Focus();
            }
        }

        private void goodsName_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.txtFKID.Focus();
            }
        }

        private void txtID_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (!string.IsNullOrEmpty(this.txtID.Text.Trim()))
                {
                    MisEntity.Goods goods = goodsBLL.EndsWithID(this.txtID.Text.Trim());
                    if (goods == null)
                    {
                        MessageBox.Show("无此商品,请先设置！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        this.txtID.Focus();
                        return;
                    }
                    this.txtGoods.Text = goods.GOODS;
                }
                this.btnOk.Focus();
            }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtDepCode.Text.Trim()))
            {
                MessageBox.Show("部门代码不能为空！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.txtDepCode.Focus();
                return;
            }
            MisModel.Depart depart = departBLL.GetById(this.txtDepCode.Text.Trim());
            if (depart == null)
            {
                MessageBox.Show("此部门无权操作！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.txtDepCode.Focus();
                return;
            }
            if (string.IsNullOrEmpty(this.txtCusCode.Text.Trim()))
            {
                MessageBox.Show("客户代码不能为空！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.txtCusCode.Focus();
                return;
            }
            MisModel.Customer customer = cusBLL.GetById(this.txtCusCode.Text.Trim());
            if (customer == null)
            {
                MessageBox.Show("无此代码或不属于本部分！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.txtCusCode.Focus();
                return;
            }
            if (string.IsNullOrEmpty(this.txtCusDepCode.Text.Trim()))
            {
                MessageBox.Show("客户部门不能为空！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.txtCusDepCode.Focus();
                return;
            }
            MisModel.CusDep cusDep = cusDepBLL.GetBy(this.txtCusCode.Text.Trim(), this.txtCusDepCode.Text.Trim());
            if (cusDep == null)
            {
                MessageBox.Show("客户部门代码不存在,或无权操作！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.txtCusDepCode.Focus();
                return;
            }
            if (string.IsNullOrEmpty(this.txtFKID.Text.Trim()))
            {
                MessageBox.Show("结算类型不能为空！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.txtFKID.Focus();
                return;
            }

            MisModel.FKType fkType = typeBLL.GetById(this.txtFKID.Text.Trim());
            if (fkType == null)
            {
                MessageBox.Show("无此代码或不属于此结算类型！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.txtFKID.Focus();
                return;
            }
            Dictionary<string, object> dictionary = new Dictionary<string, object>();
            StringBuilder sb = new StringBuilder();
            this.dataGridView1.Rows.Clear();
            sb.Append(" and M.DEP_ID=@depId");
            dictionary.Add("@depId", this.txtDepCode.Text.Trim());
            sb.Append(" and M.DATE>=@startDate");
            dictionary.Add("@startDate", this.openStarDate.Value.ToShortDateString());
            sb.Append(" and M.DATE<=@endDate");
            DateTime time = DateTime.Parse(this.openEndDate.Value.ToShortDateString());
            time = time.Date.AddDays(1);
            dictionary.Add("@endDate", time.ToShortDateString());
            string tjStr = this.comboBox2.SelectedItem.ToString();
            if ("已设置".Equals(tjStr))
            {
                sb.Append(" AND g.cus_ID=@cusId");
                sb.Append(" AND g.cus_dep=@cusDep"); 
            }
            dictionary.Add("@cusId", this.txtCusCode.Text.Trim());
            dictionary.Add("@cusDep", this.txtCusDepCode.Text.Trim());
            if (!string.IsNullOrEmpty(this.txtID.Text.Trim()))
            {
                sb.Append(" AND M.ID=@id");
                dictionary.Add("@id", this.txtID.Text.Trim());
            }
            if (!string.IsNullOrEmpty(this.goodsName.Text.Trim()))
            {
                sb.Append(" AND M.goods=@goods");
                dictionary.Add("@goods", this.goodsName.Text.Trim());
            }
            DataTable dt = goodsBLL.GetGoodsAll(sb.ToString(), dictionary, tjStr);
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    String[] DataRow = new String[30];
                    DataRow[0] = dt.Rows[i][0].ToString();
                    DataRow[1] = dt.Rows[i][1].ToString();
                    DataRow[2] = dt.Rows[i][2].ToString();
                    DataRow[3] = dt.Rows[i][3].ToString();
                    DataRow[4] = dt.Rows[i][4].ToString();
                    DataRow[5] = dt.Rows[i][5].ToString();
                    DataRow[6] = dt.Rows[i][6].ToString();
                    DataRow[7] = dt.Rows[i][7].ToString();
                    DataRow[8] = dt.Rows[i][8].ToString();
                    DataRow[9] = dt.Rows[i][9].ToString();
                    DataRow[10] = dt.Rows[i][10].ToString();
                    DataRow[11] = dt.Rows[i][11].ToString();
                    DataRow[12] = dt.Rows[i][12].ToString();
                    DataRow[13] = dt.Rows[i][13].ToString();
                    DataRow[14] = dt.Rows[i][14].ToString();
                    DataRow[15] = dt.Rows[i][15].ToString();
                    DataRow[16] = dt.Rows[i][16].ToString();
                    DataRow[17] = dt.Rows[i][17].ToString();
                    DataRow[18] = dt.Rows[i][18].ToString();
                    this.dataGridView1.Rows.Add(DataRow);
                }
                this.label17.Text = string.Format("●共{0}条", dt.Rows.Count);
                this.btnPri1.Enabled = true;
            }
            else
            {
                this.label17.Text = "●共0条";
                MessageBox.Show("查无符合条件的记录！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
        }

        private void btnPri1_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("请检查销价等项目是否正确。\n是否存盘[Y/N]？", "提问窗", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                if (this.dataGridView1.Rows.Count > 0)
                {
                    IList<MisEntity.Goods> list = new List<MisEntity.Goods>(this.dataGridView1.Rows.Count);
                    MisEntity.Goods entity = null;
                    DataGridViewRow dr = null;
                    for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
                    {
                        dr = this.dataGridView1.Rows[i];
                        entity = new MisEntity.Goods();
                        entity.ID = dr.Cells["data1"].Value.ToString();
                        entity.TYPE_JS = dr.Cells["data5"].Value.ToString();
                        entity.DAY_JS = 0;
                        entity.Price_S = decimal.Parse(dr.Cells["data3"].Value.ToString());
                        entity.Oper = accountUser.UserCode;
                        list.Add(entity);
                    }
                    goodsBLL.saveGoodsPrice(list, this.txtCusCode.Text.Trim(), this.txtCusDepCode.Text.Trim());
                    MessageBox.Show(string.Format("存盘结束！"), "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                this.dataGridView1.Rows.Clear();
                this.btnPri1.Enabled = false;
            }
        }

        private void btnQuit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void toolStripMenuItem_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("calc.exe");
        }

        private void btnPri2_Click(object sender, EventArgs e)
        {
            Point mousePoint = MousePosition;
            Point point = this.PointToClient(mousePoint);
            this.contextMenuStrip1.Show(this, point);
        }
    }
}
